package com.lc.user.express.adapter;

import android.content.Context;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.user.express.R;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.model.FurnitureModel;
import com.lc.user.express.utils.image.ImageManager;

import java.util.List;

/**
 * Created by Administrator on 2016/3/1.
 */
public class FurnitureAdapter extends BaseListAdapter<FurnitureModel> {
    private int type;

    public FurnitureAdapter(Context context, List<FurnitureModel> data, int type) {
        super(context, data);
        this.type = type;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            if (type == 1) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_furniture_list, null);
            } else if (type == 2) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_furniture_child_list, null);
            } else if (type == 3) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_furniture_child2_list, null);
            }

            AppContext.ScaleScreenHelper.loadView((ViewGroup) convertView);
            viewHolder = new ViewHolder();
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.iv_pic = (ImageView) convertView.findViewById(R.id.iv_pic);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if(type == 1 || type == 2){
            TextPaint tp = viewHolder.tv_name.getPaint();
            tp.setFakeBoldText(true);
            viewHolder.tv_name.setText(data.get(position).getName());
        }else {
            viewHolder.tv_name.setText(data.get(position).getName());
        }

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewHolder.iv_pic.getLayoutParams();
        if (position % 2 == 0) {//偶数居左，基数居右，中间留空隙，不写的话右侧会有一条白色缝隙，影响效果
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        } else {
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        }
        viewHolder.iv_pic.setLayoutParams(params);

        ImageManager.from(mContext).displayImage(viewHolder.iv_pic,data.get(position).getPicUrl(),R.drawable.shape_gay_pic_background);
        return convertView;
    }

    private class ViewHolder {
        TextView tv_name;
        ImageView iv_pic;
    }
}
